using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Musicman.Common.Entities;
using Musicman.Business;

namespace MusicmanWeb.ADMIN
{
    public partial class UsersOverview : MusicmanPage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
            List<User> users = GetUsers();
            this.UsersGV.DataSource = users;
            this.UsersGV.DataBind();
        }

        protected void UsersGV_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            User user = e.Row.DataItem as User;
            if (user != null)
            {
                if (user.Disabled)
                {
                    // disabled
                    e.Row.CssClass = "disabledusers";
                }
                else if (user.UserStats.LastLoggedDate < DateTime.Now.AddDays(-14))
                {
                    // very idle
                    e.Row.CssClass = "veryidleusers";
                }
                else if (user.UserStats.LastLoggedDate < DateTime.Now.AddDays(-7))
                {
                    // idle
                    e.Row.CssClass = "idleusers";
                }
                else
                {
                    // active
                    e.Row.CssClass = "activeusers";
                }
                e.Row.Cells[4].Text = user.UserStats.LastLoggedDate.ToString("dd-MM-yy hh:mm");
            }
        }

        protected void UsersGV_Sorting(object sender, GridViewSortEventArgs e)
        {
            GridView gv = sender as GridView;
            List<User> users = GetUsers();
            SortDirection sortDirection = e.SortDirection;
            if (ViewState[e.SortExpression] != null)
            {
                sortDirection = (SortDirection)ViewState[e.SortExpression] == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
            }
            ViewState[e.SortExpression] = sortDirection;

            if (e.SortExpression == "Firstname")
            {
                users.Sort(delegate(User u1, User u2)
                {
                    return sortDirection == SortDirection.Ascending ?
                         u1.Firstname.CompareTo(u2.Firstname) :
                         u2.Firstname.CompareTo(u1.Firstname);
                });
            }

            if (e.SortExpression == "Lastname")
            {
                users.Sort(delegate(User u1, User u2)
                {
                    return sortDirection == SortDirection.Ascending ?
                         u1.Lastname.CompareTo(u2.Lastname) :
                         u2.Lastname.CompareTo(u1.Lastname);
                });
            }

            if (e.SortExpression == "Email")
            {
                users.Sort(delegate(User u1, User u2)
                {
                    return sortDirection == SortDirection.Ascending ?
                         u1.Emailaddress.CompareTo(u2.Emailaddress) :
                         u2.Emailaddress.CompareTo(u1.Emailaddress);
                });
            }

            if (e.SortExpression == "Loggeddate")
            {
                users.Sort(delegate(User u1, User u2)
                {
                    return sortDirection == SortDirection.Ascending ?
                         u1.UserStats.LastLoggedDate.CompareTo(u2.UserStats.LastLoggedDate) :
                         u2.UserStats.LastLoggedDate.CompareTo(u1.UserStats.LastLoggedDate);
                });
            }
            gv.DataSource = users;
            gv.DataBind();
        }

        protected void UsersGV_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView gv = sender as GridView;
            List<User> files = GetUsers();
            gv.DataSource = files;
            gv.PageIndex = e.NewPageIndex;
            gv.DataBind();
        }

        private List<User> GetUsers()
        {
            if (Cache["users"] == null)
            {
                List<User> users = UserManager.Instance.LoadAllUsers();
                Cache.Insert("users",users, null, DateTime.Now.AddMinutes(3), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            return Cache["users"] as List<User>;
        }
    }
}
